package service;

import entity.*;
import model.*;
import util.ConverterTool;

import java.util.ArrayList;
import java.util.List;

public class HouseService implements ExcelInterface {
    public String importExcel(String path) {
        StringBuffer stringBuffer=new StringBuffer();
        List<HouseDataModel> houseDataModelList=ExeclService.importExcel(path,new HouseDataModel());
        if(houseDataModelList!=null){
            ArrayList<Litigation> litigationList=new ArrayList<>();
            ArrayList<LitigationBorrowerDetailed> litigationBorrowerDetaileds=new ArrayList<>();
            ArrayList<BusinessHouse> businessHouseList=new ArrayList<>();
            for (HouseDataModel houseDataModel : houseDataModelList) {
                litigationList.add(ExeclService.buildLitigationObj(houseDataModel));
                ArrayList<LitigationBorrowerDetailed> borrowerDetailedList = ExeclService.gainBorrower(houseDataModel);
                borrowerDetailedList.forEach(item->{
                    litigationBorrowerDetaileds.add(item);
                });
                BusinessHouse house=new BusinessHouse();
                house.setHouseName(houseDataModel.getHouseName());
                house.setHouseAddress(houseDataModel.getHouseAddress());
                house.setHouseNo(houseDataModel.getHouseNo());
                house.setHouseArea(ConverterTool.parseDecimal(houseDataModel.getHouseArea()));
                house.setHousePrice(ConverterTool.parseDecimal(houseDataModel.getHousePrice()));
                house.setHouseTotal(ConverterTool.parseDecimal(houseDataModel.getHouseTotal()));
                house.setButTime(houseDataModel.getButTime());
                house.setRegisterTime(houseDataModel.getRegisterTime());
                house.setHouseBelong(houseDataModel.getHouseBelong());
                house.setOpenTime(houseDataModel.getOpenTime());
                house.setBorrowTime(houseDataModel.getBorrowTime());
                house.setBorrowType(houseDataModel.getBorrowType());
                house.setBorrowTotal(ConverterTool.parseDecimal(houseDataModel.getBorrowTotal()));
                house.setBorrowBalance(ConverterTool.parseDecimal(houseDataModel.getBorrowBalance()));
                house.setFsdBankName(houseDataModel.getFsdBankName());
               businessHouseList.add(house);
            }
            stringBuffer.append(ExeclService.listToInsertSql(litigationList));
            stringBuffer.append(ExeclService.listToInsertSql(litigationBorrowerDetaileds));
            stringBuffer.append(ExeclService.listToInsertSql(businessHouseList));
        }
        return stringBuffer.toString();
    }

    public int dataType() {
        return 1;
    }
}
